/**
 * Created by lixiaodi on 2018/10/10.
 */

mini.parse();
var grid = mini.get("datagrid1");
var tree = mini.get("tree1");
//grid.load(ajaxUrl + "/CompanyController/getList.do");
//grid.load();
var url = bootPATH + "../frame/frame1/pages/";
var menu = new ColumnsMenu(grid);
//加载树
$(function(){
    treeData();
});
//点击树节点的时候显示
tree.on("nodeselect", function (e) {
    //被选中得节点
    var selecNode = e.node;
    if (selecNode) {
        ajaxBack(selecNode)
    } else {
        grid.setData([]);
        grid.setTotalCount(0);
    }
});
//增加
function add() {
    var selectNode = tree.getSelectedNode();
    //如果层级是0，则为集团节点,集团节点下才可以增加公司节点
    if(tree.getLevel(selectNode) == 0){
        mini.open({
            url: url + "alertBox/companyWindow.html",
            title: "新增公司", width: 600, height: 400,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = {
                    action: "new",
                    groupsId : selectNode.pk,
                    groupsName : selectNode.text
                };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                //子页面关闭的时候，请求当前被点击节点下的数据
                ajaxBack(selectNode);
            }
        });
    }else{
        mini.alert("当前节点不可以添加子节点");
    }

}
//编辑
function edit() {
    var row = grid.getSelected();
    var selectNode = tree.getSelectedNode();
    if (row) {
        mini.open({
            url: url + "alertBox/companyWindow.html",
            title: "编辑公司", width: 600, height: 400,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "edit", id: row.companyId };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                //子页面关闭的时候，请求当前被点击节点下的数据
                ajaxBack(selectNode);
            }
        });

    } else {
        mini.alert("请选中一条记录");
    }

}
//删除
function remove() {
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        mini.confirm("确定删除选中记录？","系统提示",function(action){
            if (action == "ok") {
                var ids = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.companyId);
                }
                var id = ids.join(',');
                grid.loading("操作中，请稍后......");
                $.ajax({
                    url: ajaxUrl + "/CompanyController/deletes.do?ids=" + id,
                    type:"post",
                    success: function (data) {
                        console.log()
                        grid.reload();
                    },
                    error: function (data) {
                    }
                });
            }
        })

    } else {
        mini.alert("请选中一条记录");
    }
}
//查询
function search() {
    var key = mini.get("key").getValue();
    grid.load({ key: key });
    $.ajax({
        url: ajaxUrl + "/CompanyController/getListByParam.do",
        type:"post",
        datatype:"json",
        data:{
            companyName:key
        },
        success: function (data) {
            grid.setData(data.data)
            //grid.reload();
        },
        error: function () {
            mini.alert("失败");
        }
    });
}
function onKeyEnter(e) {
    search();
}
//点击节点ajax请求
function ajaxBack(selectNode){
    //子页面关闭的时候，请求当前被点击节点下的数据
    $.ajax({
        url : ajaxUrl + "/CompanyController/companyListBygroupId.do",
        type : "POST",
        data : {
            id : selectNode.pk,
            types : selectNode.id.substring(0,2)
        },
        success : function(data){
            if(data.data){
                grid.setData(data.data);
            }else{
                showTips(data.msg);
                grid.setData(data.data);
            }

        },
        error : function(data){
            mini.alert(data.msg);
        }
    })
}
function treeRefresh(){
    tree.reload();
    treeData();
}

function treeData(){
    $.ajax({
        url : ajaxUrl + "/TreeController/getGrCoTree.do",
        type:"post",
        success: function (data) {
            tree.setData(data.data);
        },
        error:function(data){
            mini.alert(data.message);
        }
    })
}

